#coding=utf-8
import unittest
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.my_car import MyCar
from fixture import user
import time



class MyCarTest(LoginBaseApi):
    ''' 测试用户首页 '''

    def setUp(self):
        self.car_apply_url = "http://192.168.1.141:8001/my-car"
        self.renting_contract_mobile = '13522011855'
        self.mobile = '13651020524'
        self.password = '123abc'

    def test_user_with_contract_car(self):
        ''' 有合同用户车辆信息 '''

        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        payload =MyCar().my_car()
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)

        db_car_plate_number = user.select_car_plate_number(self.renting_contract_mobile)
        plateNumber = response.json()['plateNumber']
        self.assertEqual(db_car_plate_number, plateNumber)

        db_car_trade_time = user.select_car_trade_time(self.renting_contract_mobile)
        trade_time = str(db_car_trade_time['start_date'])+ '-' + str(db_car_trade_time['end_date'])

        start_date = time.strptime(response.json()['rentDate'][0:10], "%Y.%m.%d")
        new_start_date = time.strftime("%Y-%m-%d", start_date)

        end_date = time.strptime(response.json()['rentDate'][11:21], "%Y.%m.%d")
        new_end_date = time.strftime("%Y-%m-%d", end_date)
        renDate = new_start_date + '-' + new_end_date

        self.assertEqual(trade_time, renDate)

    def test_user_with_out_contract_car(self):
        header = LoginBaseApi().login_base_api(self.mobile, self.password)
        payload = MyCar().my_car()
        response = self.post(self.car_apply_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 200)
        self.assertEqual(response.text, '')




if __name__ == '__main__':

    unittest.main()
